package advertising

import (
	"context"
	"strings"

	"advertising/config/mysql"
	"advertising/internal/app/grpc/protoc/advertisingc"
	"advertising/internal/app/web/service"
	"advertising/internal/dao"
	"advertising/internal/models"
)

// MaterialList 素材列表
func (Server) MaterialList(ctx context.Context, req *advertisingc.MaterialListReq) (*advertisingc.MaterialListRsp, error) {
	res := &advertisingc.MaterialListRsp{}
	res.Code = -1
	if req.Type != 0 && req.Type != 1 && req.Type != 2 {
		res.Msg = "素材类型错误"
		return res, nil
	}
	if req.Position != 0 && req.Position != 1 && req.Position != 2 {
		res.Msg = "素材位置错误"
		return res, nil
	}
	if req.Status < 0 || req.Status > 4 {
		res.Msg = "素材状态错误"
		return res, nil
	}

	TypeSlice := []int8{}
	if req.Type != 0 {
		TypeSlice = []int8{int8(req.Type)}
	}
	if req.Type != 1 { // 图片才能过滤前后屏
		req.Position = 0
	}
	var StatusString []int8
	if req.Status == 0 {
		StatusString = []int8{2, 3, 4} // 1 等待审核 2 审核通过 3 禁用 4 审核未通过
	} else {
		StatusString = []int8{int8(req.Status)}
	}
	MaterialList, count, _ := dao.Material{}.List(ctx, mysql.NewDB(), req.MerchantId, "", req.Name, TypeSlice, StatusString,
		req.CreateUsername, int(req.Position), int(req.Page), int(req.Limit), req.FolderId)
	res.Data = &advertisingc.MaterialListRsp_Data{}
	res.Data.Count = count
	for _, v := range MaterialList {
		var item = &advertisingc.MaterialListRsp_Data_MaterialList{}
		item.Id = v.ID
		item.Name = v.Name
		item.Type = int32(v.Type)
		item.Position = int32(v.Position)
		item.Duration = v.Duration
		item.Suffix = v.Suffix
		item.Size = v.Size
		item.Status = int32(v.Status)
		item.FilePath = v.FilePath
		item.MerchantId = v.MerchantID
		item.MerchantName = v.MerchantName
		item.CreateUser = v.CreateUser
		item.CreateUsername = v.CreateUserName
		item.CreatedAt = v.CreatedAt
		item.DeletedAt = v.DeletedAt
		res.Data.List = append(res.Data.List, item)
	}
	res.Code = 0
	return res, nil
}

// MaterialAudit 素材审核
func (Server) MaterialAudit(ctx context.Context, req *advertisingc.MaterialAuditReq) (*advertisingc.MaterialAuditRsp, error) {
	res := &advertisingc.MaterialAuditRsp{}
	res.Code = -1
	if req.Id == "" {
		res.Msg = "素材不能为空"
		return res, nil
	}
	if req.Status != 2 && req.Status != 4 {
		res.Msg = "素材审核类型错误"
		return res, nil
	}
	db := mysql.NewDB()
	MaterialInfo, count := dao.Material{}.Detail(ctx, db, "", req.Id, "", 0)
	if count == 0 {
		res.Msg = "素材不存在"
		return res, nil
	}
	if MaterialInfo.Status != 1 { //等待审核，审核中
		res.Msg = "素材当前状态无法进行审核操作"
		return res, nil
	}

	// 修改素材状态
	dao.Material{}.ChangeStatus(ctx, db, "", req.Id, []int8{}, int8(req.Status))

	//写入审核记录
	dao.MaterialAuditLog{}.Create(ctx, db, req.Id, MaterialInfo.MerchantID, MaterialInfo.MerchantName,
		req.MerchantId, req.MerchantName, req.CreateUser, req.CreateUsername, int(req.Status/2)) // 2 => 1 通过, 4 => 2拒绝
	res.Code = 0
	return res, nil
}

// MaterialSwitch 素材禁用启用，支持批量
func (Server) MaterialSwitch(ctx context.Context, req *advertisingc.MaterialSwitchReq) (*advertisingc.MaterialSwitchRsp, error) {
	res := &advertisingc.MaterialSwitchRsp{}
	res.Code = -1
	if req.Id == "" {
		res.Msg = "素材不能为空"
		return res, nil
	}
	if req.Status != 2 && req.Status != 3 {
		res.Msg = "素材审核类型错误"
		return res, nil
	}

	db := mysql.NewDB()
	dao.Material{}.ChangeStatus(ctx, db, "", req.Id, []int8{2, 3}, int8(req.Status)) //禁用启用状态
	res.Code = 0
	return res, nil
}

type AuditLogListStruct struct {
	models.Material
	AuditMerchantID   string `gorm:"column:audit_merchant_id"`
	AuditMerchantName string `gorm:"column:audit_merchant_name"`
	AuditUser         string `gorm:"column:audit_user"`
	AuditUsername     string `gorm:"column:audit_username"`
	AuditStatus       int32  `gorm:"column:audit_status"`
	AuditAt           int32  `gorm:"column:audit_at"`
}

// MaterialAuditLogList 审核记录列表
func (Server) MaterialAuditLogList(ctx context.Context, req *advertisingc.MaterialAuditLogListReq) (*advertisingc.MaterialAuditLogListRsp, error) {
	res := &advertisingc.MaterialAuditLogListRsp{}

	db := mysql.NewDB()
	var MaterialIDList []string
	var materialMap = make(map[string]models.Material)
	materialList, _, _ := dao.Material{}.List(ctx, db, "", "", req.Name, []int8{}, []int8{}, "", 0, 0, 0, "")
	for _, v := range materialList {
		MaterialIDList = append(MaterialIDList, v.ID)
		materialMap[v.ID] = *v
	}

	res.Data = &advertisingc.MaterialAuditLogListRsp_Data{}
	AuditLogList, count, _ := dao.MaterialAuditLog{}.List(ctx, db, strings.Join(MaterialIDList, ","), int(req.Page), int(req.Limit))
	for _, v := range AuditLogList {
		var item = &advertisingc.MaterialAuditLogListRsp_Data_MaterialAuditLogList{}
		item.Id = v.ID
		item.AuditMerchantId = v.AuditMerchantID
		item.AuditMerchantName = v.AuditMerchantName
		item.AuditUser = v.AuditUser
		item.AuditUsername = v.AuditUsername
		item.AuditAt = int32(v.AuditAt)
		item.AuditStatus = int32(v.AuditStatus)
		if material, ok := materialMap[v.MaterialID]; ok {
			item.FilePath = material.FilePath
			item.Name = material.Name
			item.Type = int32(material.Type)
			item.Duration = material.Duration
			item.Suffix = material.Suffix
			item.Size = material.Size
			item.CreatedAt = material.CreatedAt
			item.MerchantId = material.MerchantID
			item.MerchantName = material.MerchantName
			item.CreateUser = material.CreateUser
			item.CreateUsername = material.CreateUserName
		}
		res.Data.List = append(res.Data.List, item)
	}
	res.Data.Count = count
	return res, nil
}

// MaterialFolderList 素材文件夹列表
func (Server) MaterialFolderList(ctx context.Context, req *advertisingc.MaterialFolderListReq) (*advertisingc.MaterialFolderListRsp, error) {
	res := &advertisingc.MaterialFolderListRsp{}
	folders, _ := service.MaterialFolder{}.FolderList(ctx, req.MerchantId, int8(req.Type))
	for _, v := range folders {
		var item = &advertisingc.MaterialFolderListRsp_FolderList{}
		item.FolderId = v.ID
		item.FolderName = v.Name
		item.Pid = v.PID
		if len(v.Children) > 0 {
			for _, v2 := range v.Children {
				var item2 = &advertisingc.MaterialFolderListRsp_FolderList{}
				item2.FolderId = v2.ID
				item2.FolderName = v2.Name
				item2.Pid = v2.PID
				item.Children = append(item.Children, item2)
			}
		}
		res.List = append(res.List, item)
		res.Total++
	}
	return res, nil
}
